package com.swordxu.yim.server.session;

import io.netty.channel.Channel;

import java.io.Serializable;

/**
 * YIM会话管理类
 * Created by user063 on 2014/12/23.
 */
public class YimSession implements Serializable {
    private static final long serialVersionUID = 1L;

    private Channel session;        //session channel
    public String HOST = "host";

    private String gid;             //session全局ID
    private int nid;                //session在本台服务器上的ID
    private String deviceId;        //客户端设备ID
    private String host;            //session绑定的服务器IP
    private String account;         //session绑定的账号
    private String deviceModel;     //终端设备型号

    private Long bindTime;          //登录时间
    private Long heartbeat;         //心跳时间

    /**
     * 构造方法
     * @param session
     */
    public YimSession(Channel session) {
        this.session = session;
    }

    /**
     * 判断会话是否处于连接状态
     * @return
     */
    public boolean isConnected() {
        if (session != null) {
            return session.isActive();
        }
        return false;
    }

    /**
     * 发送消息
     * @param msg
     * @return
     */
    public boolean write(Object msg) {
        if(session!=null && isConnected())
        {
            return session.write(msg).awaitUninterruptibly(5000);
        }
        return false;
    }
}
